<?php
require_once ('./Static/ArrayAssoc.php');
class ArticleModel{
	private $m_db;
	
	//sätter m_db till medskickat mysqli objekt vid instansierande av klassen
	public function __construct(Mysqli $db){
		$this->m_db = $db;
	}
	//returnerar array med alla artiklar från databasen
	public function GetArticleArray(){
		$articleArray = array();
		$stmt = $this->m_db->prepare("SELECT t1.headline, t1.article, t1.date, t2.username FROM `article` t1, `user` t2 WHERE t2.`userId`=t1.`userId` ORDER BY t1.articleId DESC;");
		$stmt->execute();
		$stmt->bind_result($headline, $article, $date, $username);
		
		while($stmt->fetch()){
			$articleArray[] = array(ArrayAssoc::ARTICLE_HEADLINE => $headline, ArrayAssoc::ARTICLE_DESC => $article, ArrayAssoc::ARTICLE_DATE => $date, ArrayAssoc::ARTICLE_WRITER => $username);
		}
		return $articleArray;
	}
	//returnerar array med enskild artiklar från databasen
	public function GetArticle($articleId){
		$stmt = $this->m_db->prepare("SELECT t1.headline, t1.article, t1.date, t2.username FROM `article` t1, `user` t2 WHERE t1.`articleId`=? AND t2.`userId`=t1.`userId` LIMIT 1;");
		$stmt->bind_param("i", $articleId); 
		$stmt->execute();
		$stmt->bind_result($headline, $article, $date, $username);
		
		if($stmt->fetch()){
			return array(ArrayAssoc::ARTICLE_HEADLINE => $headline, ArrayAssoc::ARTICLE_DESC => $article, ArrayAssoc::ARTICLE_DATE => $date, ArrayAssoc::ARTICLE_WRITER => $username);
		}		
		return null;		
	}
}
?>